package com.wy.netty.tcp.client;

import java.time.Duration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 
 *
 * @author 飞花梦影
 * @date 2024-04-28 17:26:07
 * @git {@link https://github.com/dreamFlyingFlower}
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

	private static final String CACHE_PREFIX = "netty-tcp-demo";

	@Value("${spring.redis.ttl:120}")
	private Long cacheTtl;

	@Bean
	@SuppressWarnings(value = { "unchecked", "rawtypes" })
	public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
		RedisTemplate<Object, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(connectionFactory);

		FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);

		// 使用StringRedisSerializer来序列化和反序列化redis的key值
		template.setKeySerializer(new KeyStringRedisSerializer(CACHE_PREFIX));
		template.setValueSerializer(serializer);

		// Hash的key也采用StringRedisSerializer的序列化方式
		template.setHashKeySerializer(new KeyStringRedisSerializer(CACHE_PREFIX));
		template.setHashValueSerializer(serializer);

		template.afterPropertiesSet();
		return template;
	}

	@Bean
	public RedisCacheManager redisCacheManager(RedisConnectionFactory connectionFactory) {
		RedisCacheConfiguration cacheConfiguration =
				RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(cacheTtl)) // 缓存时长
						.disableCachingNullValues();
		return RedisCacheManager.builder(connectionFactory).cacheDefaults(cacheConfiguration).build();
	}
}